package com.bwie.task;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.bwie.mapper.*;
import com.bwie.mq.EndTimeProducer;
import com.bwie.mq.EwMonthProducer;
import com.bwie.mq.MessageProducer;
import com.bwie.pojo.po.Charging;
import com.bwie.pojo.po.Customer;
import com.bwie.pojo.po.OrderPark;
import com.bwie.pojo.po.User;
import com.bwie.pojo.vo.MonthCarVo;
import com.bwie.pojo.vo.OrderParkVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Component
public class SpringTaskServer {
    @Autowired
    private MonthCarMapper monthCarMapper;


    @Autowired
    private MessageProducer messageProducer;
    @Autowired
    private OrderParkMapper orderParkMapper;
    @Autowired
    private ChargingMapper chargingMapper;
    @Autowired
    private EwMonthProducer ewMonthProducer;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private EndTimeProducer endTimeProducer;

//    @Scheduled(cron = "*/30 * * * * ?")
//    public void sout1(){
//        System.out.println("aaaaaaaaaaaa"+new Date());
//    }

    @Scheduled(cron = "0 0 2 * * ?")
    public void sout2(){
        //修改有效天数
        monthCarMapper.updTimes();
        //天数小于0修改状态
        monthCarMapper.updState();
    }
    //1.编写定时任务，每天早上8点执行
    @Scheduled(cron = "0 */1 21 * * ?")
    public void sout3(){
        //2.定时查询数据库的快到期的月卡信息
        List<MonthCarVo> list = monthCarMapper.selectMonthCatByTime();
        for (MonthCarVo monthCarVo : list) {
            Integer userId = monthCarVo.getUserId();
            User user = userMapper.getUserById(userId);
            messageProducer.sengMsgToMq(user);

        }
    }



    @Scheduled(cron = "0 */1 21 * * ?")
    public void sout4(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<OrderParkVo> respList = new ArrayList<>();
        List<OrderPark> list = orderParkMapper.getOrderParkBiType();
        for (OrderPark orderPark : list) {
            if (orderPark.getEndTime().getTime()<new Date().getTime()){
                OrderParkVo orderParkVo = new OrderParkVo();
                BeanUtils.copyProperties(orderPark,orderParkVo);
                long min = DateUtil.between(orderPark.getEndTime(), new Date(), DateUnit.MINUTE);
                long hours = min / 60;
                long min1 = min % 60;
                if (min1>=30){
                    hours+=1;
                }


                //计算额外费用金额（超时部分）
                Charging charging = chargingMapper.selectChargingById(orderPark.getParkId(),orderPark.getCarType());
                BigDecimal chargingPrice = charging.getChargingPrice();
                BigDecimal bigDecimal = new BigDecimal(hours);
                BigDecimal ewMonty = bigDecimal.multiply(chargingPrice);
                orderParkVo.setEwMoney(ewMonty);
                ewMonthProducer.toSms(orderParkVo);
            }
        }
    }


    /**
     * 跟进提醒
     */
    @Scheduled(cron = "0 41 10 * * ?")
    public void sout5(){
        //获取长时间未跟进的客户
        List<Customer> list = customerMapper.getCustomerEndTime();

        for (Customer customer : list) {
            endTimeProducer.endTime(customer);
        }
    }








}
